#include "addwidge.h"
#include "ui_addwidge.h"
#include "widget.h"
#include "QSqlQuery"
#include "widget.h"
AddWidge::AddWidge(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddWidge)
{
    ui->setupUi(this);
}

AddWidge::~AddWidge()
{
    delete ui;
}
void AddWidge::on_AddButton_clicked()
{
    QString userName=ui->UsnInput->text();
    QString passWord=ui->PsdInput->text();
    QSqlQuery request;
    request.prepare("select username from user where username=?");
    request.addBindValue(userName);
    request.exec();
    if(request.next() && request.value(0).toString()==userName)
    {
        QMessageBox::warning(this,"error","userName is exist");
    }
    else
    {
        request.prepare("insert into user(username,password,last_login_time) values(?,?,?)");
        request.addBindValue(userName);
        request.addBindValue(passWord);
        QDateTime now = QDateTime::currentDateTime();
        QString   current = now.toString("yyyy-MM-dd HH:mm:ss");
        request.addBindValue(current);
        request.exec();
        QMessageBox::warning(this,"success","add success");
        ui->UsnInput->setText("");
        ui->PsdInput->setText("");
        emit sendMsg();
    }
}
